package com.example.myapplication5;

import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static int MODE = MODE_PRIVATE;
    public static final String PREFERENCE_NAME = "SaveSetting";
    private Button btnLogin;
    private EditText etAccount, etPassword;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);

        // 加载保存的数据
        sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        String savedAccount = sharedPreferences.getString("account", "");
        String savedPassword = sharedPreferences.getString("password", "");
        etAccount.setText(savedAccount);
        etPassword.setText(savedPassword);

        // 设置按钮点击监听
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_login) {
            String account = etAccount.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            // 保存数据到SharedPreferences
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("account", account);
            editor.putString("password", password);
            editor.apply();

            // 提示保存成功
            Toast.makeText(this, "登录成功，已保存账号密码", Toast.LENGTH_SHORT).show();
        }
    }
}